package hoyo.zqy.system.service.impl;

import hoyo.zqy.system.domain.Role;
import hoyo.zqy.system.mapper.RoleMapper;
import hoyo.zqy.system.service.IRoleService;
import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void save(Role role) {

        super.save(role);
        //添加当前角色的权限 - 前端一定是选择了权限
        if(role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addPermissons(role);
        }
        //添加当前角色的菜单 - 前端一定是选择了菜单
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addMenus(role);
        }

    }

    @Override
    @Transactional
    public void delete(Long id) {
        //删除角色所有权限
        roleMapper.delPermissByRoleId(id);
        //删除角色菜单权限
        roleMapper.delMenuRoleById(id);

        super.delete(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        //删除角色菜单权限
        roleMapper.delMenuRoleById(role.getId());
        //删除角色所有权限
        roleMapper.delPermissByRoleId(role.getId());
        if (role.getPermissions()!=null && role.getPermissions().size()>0){
            //添加角色所有权限
            roleMapper.addPermissons(role);
        }
        if (role.getMenus()!=null && role.getMenus().size()>0){
            //添加角色菜单权限
            roleMapper.addMenus(role);
        }
        super.update(role);
    }

    @Override
    public void deleBatch(Long[] ids) {

        for (Long id : ids) {
            delete(id);
        }
    }
}
